﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IDAL;
using Model;


namespace SQLServerDAL
{
    public class FilmTreeDAL : IFilmTreeDAL
    {
         public Boolean InsertFilmTree(FilmTree filmTree)
        {
            try
            {
                
                FilmTreeDataClassesDataContext filmTreeContext = new FilmTreeDataClassesDataContext();
                filmTreeContext.FilmTrees.InsertOnSubmit(filmTree);
                filmTreeContext.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        public Boolean DeleteFilmTree(int filmListId)
        {
            try
            {
                FilmTreeDataClassesDataContext filmTreeContext = new FilmTreeDataClassesDataContext();
                FilmTree filmTree = (from u in filmTreeContext.FilmTrees
                             where u.FilmListId.Equals(filmListId)
                             select u).First();
                filmTreeContext.FilmTrees.DeleteOnSubmit(filmTree);
                filmTreeContext.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        public Boolean UpdateFilmTree(int filmListId, FilmTree filmTree)
        {

            if (filmListId != filmTree.FilmListId)
            {
                return false;
            }
            FilmTreeDataClassesDataContext filmTreeContext = new FilmTreeDataClassesDataContext();
            FilmTree query = (from u in filmTreeContext.FilmTrees
                          where u.FilmListId.Equals(filmListId)
                          select u).First();
            //query.Password = filmTree.Password;

            filmTreeContext.SubmitChanges();
            return true;

        }

        public FilmTree SelectFilmTreeById(int filmListId)
        {
            FilmTreeDataClassesDataContext filmTreeContext = new FilmTreeDataClassesDataContext();
            FilmTree filmTree = (from u in filmTreeContext.FilmTrees
                         where u.FilmListId.Equals(filmListId)
                         select u).First();
            return filmTree;
        }


        public List<FilmTree> SelectAllFilmTree()
        {
            FilmTreeDataClassesDataContext filmTreeContext = new FilmTreeDataClassesDataContext();
            var query = (from u in filmTreeContext.FilmTrees
                         select u);
            List<FilmTree> filmTreeList = new List<FilmTree>();

            foreach (var filmTree in query)
            {
                filmTreeList.Add(filmTree);
            }
            return filmTreeList;
        }

    }
}
